package com.sandking.db.jedis;

import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import java.util.List;
import com.sandking.db.bean.Gly;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;
import com.sandking.db.dao.GlyDao;

public class GlyJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Gly getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Gly gly = null;
		try{
			String key = "Gly_Object";
			String field = SK_Plus.b("id:",id).e();
			gly = Gly.createForJson(jedis.hget(key,field));
			if(gly == null){
				gly = GlyDao.getById(id);
				if(gly!=null){
					loadCache(gly);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return gly;
	}
	
	/**
	 * 根据( 账号  密码 ) 查询
	 */
	public static Gly getByZhMm(String zh, String mm){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Gly gly = null;
		try{
			String key = "Gly_Index";
			String field = SK_Plus.b("zh, mm:",zh, mm).e();
			String primaryKey = jedis.hget(key,field);
			if(primaryKey!=null){
				gly = getById(Integer.valueOf(primaryKey));
			}
			if(gly == null){
				gly = GlyDao.getByZhMm(zh, mm);
				if(gly!=null){
					loadCache(gly);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return gly;
	}
	

	public static void loadCache(Gly gly){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(gly,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Gly gly,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Gly_Object";
		String field = SK_Plus.b("id:",gly.getId()).e();
		String data = gly.toJson();
		p.hset(key,field,data);
		
		// ---------------------- 唯一索引 ----------------------
		String primaryKey = String.valueOf(gly.getId());
		key = "Gly_Index";
		
		field = SK_Plus.b("zh, mm:",gly.getZh(),gly.getMm()).e();
		p.hset(key,field,primaryKey);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Gly> glys){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Gly gly : glys){
				loadCache(gly,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Gly gly){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(gly,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Gly gly,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Gly_Object";
		String field = SK_Plus.b("id:",gly.getId()).e();
		p.hdel(key,field);
		
		// ---------------------- 唯一索引 ----------------------
		key = "Gly_Index";
		
		field = SK_Plus.b("zh, mm:",gly.getZh(),gly.getMm()).e();
		p.hdel(key,field);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Gly> glys){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Gly gly : glys){
				clearCache(gly,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Gly insert(Gly gly){
		gly = GlyDao.insert(gly);
		if(gly!=null){
			loadCache(gly);
		}
    	return gly;
    }
    
    public static Gly update(Gly gly){
    	gly = GlyDao.update(gly);
    	if(gly!=null){
			loadCache(gly);
		}
    	return gly;
    }
    
    public static boolean delete(Gly gly){
    	boolean bool = GlyDao.delete(gly);
    	if(bool){
    		clearCache(gly);
    	}
    	return bool;
    }
}